﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CIT.WMS.Other
{
    public partial class FrmFordBillCheck : Form
    {
        public FrmFordBillCheck()
        {
            InitializeComponent();
        }

        private void clearBillLbl()
        {
            areaMsg.Text = "条码格式不正确，请确认！";
            txtBillLablel.Text = "";
            txtBillLablel.Focus();
        }
        private void clearPalletLbl()
        {
            areaMsg.Text = "条码格式不正确，请确认！";
            this.txtPalletLablel.Text = "";
            this.txtPalletLablel.Focus();
        }
        private string CheckString = "";
        private string _s7DC = "";
        private string _s8PN = "";
        private string _s9LOC1 = "";
        private string _s9LOC2 = "";
        private string _STID = "";

        private void txtPalletLablel_KeyPress(object sender, KeyPressEventArgs e)
        {
            areaInfo.Text = "";
            if (e.KeyChar != 13)
            {
                return;
            }

            try
            {
                var snArray = txtPalletLablel.Text.Split(',');
                if (snArray.Length != 11)
                {
                    splitBoxLabel(txtPalletLablel.Text);
                    return;
                }

                var s7DC = snArray[7];
                var s8PN = snArray[8];
                var s9LOC1 = snArray[9];
                var s9LOC2 = snArray[10];
                var palletString = s7DC + s8PN + s9LOC1 + s9LOC2;

                if (s7DC.Contains(_s7DC) && s8PN.Contains(_s8PN) &&
                    s9LOC1.Contains(_s9LOC1) && s9LOC2.Contains(_s9LOC2))
                {
                    areaInfo.Text = "Validation \r\nOK ";
                }
                else
                {
                    areaInfo.Text = "Validation \r\nNG";
                    MessageBox.Show("NG");
                }
                clearBillLbl();
                clearPalletLbl();
                areaMsg.Text = "";
            }
            catch
            {
                clearPalletLbl();
                return;
            }
        }

        private bool splitBoxLabel(string str)
        {
            try
            {
                str = str.Substring(6);
                var idx1 = str.IndexOf("Q");
                CheckString = str.Substring(0, idx1);
                var nextStr = str.Substring(idx1);

                //--S
                var idx2 = nextStr.IndexOf("8V");
                nextStr = nextStr.Substring(idx2);

                var idx3 = nextStr.IndexOf("B");
                CheckString += nextStr.Substring(0, idx3);
                //--E

                //--S
                var idx4 = nextStr.IndexOf("1L");
                nextStr = nextStr.Substring(idx4);

                var idx5 = nextStr.IndexOf("3S");
                CheckString += nextStr.Substring(0, idx5);
                //--E

                //--S
                var idx6 = nextStr.IndexOf("20L");
                nextStr = nextStr.Substring(idx6);

                CheckString += nextStr;
                if (CheckString.Contains(_s7DC) && CheckString.Contains(_s8PN) &&
                    CheckString.Contains(_s9LOC1) && CheckString.Contains(_s9LOC2))
                {
                    areaInfo.Text = "Validation \r\nOK ";
                }
                else
                {
                    areaInfo.Text = "Validation \r\nNG";
                    MessageBox.Show("NG");
                }
                clearBillLbl();
                clearPalletLbl();
                areaMsg.Text = "";

                //--E
                return true;
            }
            catch
            {
                clearPalletLbl();
                return false;
            }
        }

        private void txtBillLablel_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 13)
            {
                return;
            }

            try
            {
                _s7DC = "";
                _s8PN = "";
                _s9LOC1 = "";
                _s9LOC2 = "";
                _STID = "";

                var sql = string.Format("SELECT * FROM wms_T_OutStock WHERE BillNo='{0}';", this.txtBillLablel.Text);
                var dt = Utils.NMS.GetDataTable(sql);
                if (dt.Rows.Count == 0)
                {
                    clearBillLbl();
                    areaMsg.Text = "订单不存在";
                }
                var sono = dt.Rows[0]["RelatedBill"].ToString().Trim();
                var solineno = dt.Rows[0]["RowNo"].ToString().Trim();
                var seq = dt.Rows[0]["SN"].ToString().Trim();

                sql = @"SELECT  * FROM OPENQUERY(ERPBU_LINK, 
                'select * from wms_ttddev523105view where sono=''{0}'' and solineno=''{1}'' and seq=''{2}''')
                 AS derivedtbl_1";
                sql = string.Format(sql, sono, solineno, seq);
                var dtErp = Utils.NMS.GetDataTable(sql);
                if (dtErp.Rows.Count == 0)
                {
                    sql = @"SELECT  * FROM OPENQUERY(ERPBU_LINK, 
                'select * from wms_ttddev523105view where sono=''{0}'' and solineno=''{1}'' ORDER BY seq DESC')
                 AS derivedtbl_1";
                    sql = string.Format(sql, sono, solineno);
                    dtErp = Utils.NMS.GetDataTable(sql);
                    if (dtErp.Rows.Count == 0)
                    {
                        clearBillLbl();
                        areaMsg.Text = "没有查询到关联信息";
                        return;
                    }
                }

                _s7DC = dtErp.Rows[0]["t_rece"].ToString().Trim();
                _s8PN = dtErp.Rows[0]["cust_item"].ToString().Trim();
                _s9LOC1 = dtErp.Rows[0]["t_rslf"].ToString().Trim();
                _s9LOC2 = dtErp.Rows[0]["t_deli"].ToString().Trim();
                _STID = dtErp.Rows[0]["stid"].ToString().Trim();
                this.txtPalletLablel.Focus();
            }
            catch
            {
                clearBillLbl();
                areaMsg.Text = "异常";
                return;
            }
        }
        //END
    }
}